import React, { Component } from 'react'
import { hasToken } from 'utils/token'
import { Route, Redirect } from 'react-router-dom'
export default class AuthRoute extends Component {
  render() {
    const { component: Component, ...rest } = this.props
    // Component 需要鉴权的目标组件  rest Route有关参数  path 等等

    if (hasToken()) {
      return (
        // Route中 component属性 与 render 属性 互斥
        // <Route path="/home" 等等额外属性 ></Route>
        <Route
          {...rest}
          // 匹配到路径渲染 返回值 Component
          render={(props) => {
            // 把一些路由组件的信息给到该组件内部   history match location等等
            return <Component {...props}></Component>
          }}
        ></Route>
      )
    } else {
      const pathname = this.props.location.pathname
      return (
        <Redirect
          to={{
            pathname: '/login',
            state: { pathname },
          }}
        ></Redirect>
      )
    }
  }
}
